package com.yoyo.yyadsdk.util;

import com.anye.greendao.gen.LocalTimingEntityDao;
import com.yoyo.yyadsdk.bean.dao.LocalTimingEntity;

import java.util.Calendar;

public class AlarmUtils {

    private static void addTaskToDB(int type, String param, long timeInMillis, boolean isLoop, long period) {
        LocalTimingEntityDao localTimingEntityDao = GreenDaoManager.getInstance().getDaoSession().getLocalTimingEntityDao();
        LocalTimingEntity localTimingEntity = new LocalTimingEntity();
        localTimingEntity.setTime(timeInMillis);
        localTimingEntity.setType(type);
        localTimingEntity.setLoop(isLoop);
        localTimingEntity.setParam(param);
        localTimingEntity.setPeriod(period);
        localTimingEntityDao.insertOrReplace(localTimingEntity);
    }

    public static void addTask(int type, String param, long delay, boolean isLoop, long period) {
        addTaskToDB(type, param, System.currentTimeMillis() + delay, isLoop, period);
    }

    public static void addTask(int type, String param, int hour, int minute, boolean isLoop, long period) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        Integer hHmm = TimeUtils.getHHmm();
        if (hHmm != null && hHmm > hour * 100 + minute) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        long timeInMillis = calendar.getTimeInMillis();
        addTaskToDB(type, param, timeInMillis, isLoop, period);
    }
}
